﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Oxytocin
{
    public partial class HistoryOxytocin : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 构造函数
        public HistoryOxytocin(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            //成员变量
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            //绑定数据
            BindControl();
            BindInfo();
        }
        #endregion


        #region 查询
        private void btnchaxun_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 新增
        private void btnxinzeng_Click(object sender, EventArgs e)
        {
            AddOxytocin addOxt = new AddOxytocin(LoginUser, PatientInfo, string.Empty);
            addOxt.ShowDialog();
            if (addOxt.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void Btnxiugai_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddOxytocin addOxt = new AddOxytocin(LoginUser, PatientInfo, Id);
                addOxt.ShowDialog();
                if (addOxt.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            PrintOxytocin po = new PrintOxytocin(LoginUser, PatientInfo, StatDate.DateTime, EndDate.DateTime);
            po.ShowDialog();
        }
        #endregion

        #region 删除
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    var ret2 = formService.DeleteNursingFormRecord(id);
                    MessageBox.Show(ret2.M);
                    if (ret2.Code == 1)
                    {
                        BindInfo();
                    }
                }
            }
        }
        #endregion

        #region 查询共用方法

        #region  开始日期：当天-30天，结束日期：为当天日期
        public void BindControl()
        {
            this.StatDate.DateTime = DateTime.Now.AddDays(-30);
            this.EndDate.DateTime = DateTime.Now;
        }
        #endregion

        public void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.Oxytocins;    //表单ID
            DataTable dt = new DataTable();
            dt.Columns.Add("RiQi", typeof(string));//日期
            dt.Columns.Add("ShiJian", typeof(string));//时间
            dt.Columns.Add("TaiCi", typeof(string));//胎次
            dt.Columns.Add("ChanCi", typeof(string));//产次
            dt.Columns.Add("ZhenDuan", typeof(string));//诊断
            dt.Columns.Add("CuiChanSu", typeof(string));//催产素
            dt.Columns.Add("JiaCuiChanSu", typeof(string));//加催产素
            dt.Columns.Add("MeiFenZhongDiShu", typeof(string));//每分钟滴数
            dt.Columns.Add("GongSuoQiangDu", typeof(string));//宫缩强度
            dt.Columns.Add("GongSuoChiXu", typeof(string));//宫缩持续
            dt.Columns.Add("GongSuoJianGe", typeof(string));//宫缩间隔
            dt.Columns.Add("GongKouWeiZhi", typeof(string));//宫口位置
            dt.Columns.Add("GongKouZhiDi", typeof(string));//宫口质地
            dt.Columns.Add("GongKouDaXiao", typeof(string)); //宫口大小
            dt.Columns.Add("XianLuGaoDi", typeof(string));//先露高低
            dt.Columns.Add("TaiMo", typeof(string));//胎膜
            dt.Columns.Add("TaiXin", typeof(string));//胎心
            dt.Columns.Add("XueYa_Zuo", typeof(string));//血压_左
            dt.Columns.Add("XueYa_You", typeof(string));//血压_右
            dt.Columns.Add("BeiZhu", typeof(string));//备注
            dt.Columns.Add("QianMing", typeof(string)); //签名
            dt.Columns.Add("Id", typeof(string));//ID隐藏，双击触发事件
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, StatDate.DateTime, EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    OXT ox = new OXT();
                    ox = XmlUtil.XMLToObject<OXT>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["RiQi"] = Convert.ToDateTime(ox.RiQi).ToString("yyyy-MM-dd");     //日期
                    newRow["ShiJian"] = DateTimeHelper.HHmmssToHHmm(ox.ShiJian);     //时间
                    newRow["TaiCi"] = ox.TaiCi;     //胎次
                    newRow["ChanCi"] = ox.ChanCi;     //产次
                    newRow["ZhenDuan"] = ox.ZhenDuan;     //诊断
                    newRow["CuiChanSu"] = ox.CuiChanSu;     //催产素
                    newRow["JiaCuiChanSu"] = ox.JiaCuiChanSu;     //加催产素
                    newRow["MeiFenZhongDiShu"] = ox.MeiFenZhongDiShu;     //每分钟滴数
                    newRow["GongSuoQiangDu"] = ox.GongSuoQiangDu;     //宫缩强度
                    newRow["GongSuoChiXu"] = ox.GongSuoChiXu;     //宫缩持续
                    newRow["GongSuoJianGe"] = ox.GongSuoJianGe;     //宫缩间隔
                    newRow["GongKouWeiZhi"] = ox.GongKouWeiZhi;     //宫口位置
                    newRow["GongKouZhiDi"] = ox.GongKouZhiDi;     //宫口质地
                    newRow["GongKouDaXiao"] = ox.GongKouDaXiao;     //宫口大小
                    newRow["XianLuGaoDi"] = ox.XianLuGaoDi;     //先露高低
                    newRow["TaiMo"] = ox.TaiMo;     //胎膜
                    newRow["TaiXin"] = ox.TaiXin;     //胎心
                    newRow["XueYa_Zuo"] = ox.XueYa_Zuo;     //血压_左
                    newRow["XueYa_You"] = ox.XueYa_You;     //血压_右
                    newRow["BeiZhu"] = ox.BeiZhu;     //备注
                    newRow["QianMing"] = ox.QianMing;     //签名
                    newRow["Id"] = record.Id;   //ID隐藏，双击触发事件
                    dt.Rows.Add(newRow);
                }
            }
            dt.DefaultView.Sort = "RiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                if (bandedGridView1.RowCount > 0)
                {
                    string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    AddOxytocin addy = new AddOxytocin(LoginUser, PatientInfo, Id);
                    addy.ShowDialog();
                    if (addy.DialogResult == DialogResult.OK)
                    {
                        BindInfo();
                    }
                }
            }
        }
        #endregion

    }
}
public class DateTimeHelper
{
    /// <summary>
    /// 将时分秒 转换为 时分
    /// 并为时间单个数值补0
    /// 此函数作为显示数据的时候调用
    /// 保存数据请用DateTimeTran.TimeFillZero函数
    /// </summary>
    /// <param name="time">HH:mm:dd</param>
    /// <returns>HH:mm</returns>
    public static string HHmmssToHHmm(string time)
    {
        string res = string.Empty;
        if (string.IsNullOrEmpty(time)) return res;
        if (time.IndexOf(":") < 0) return res;
        string[] timeArr = time.Split(':');
        if (timeArr.Length <= 2) return DateTimeTran.TimeFillZero(time);
        if (timeArr.Length > 2)
        {
            string hhmm = timeArr[0] + ":" + timeArr[1];
            return DateTimeTran.TimeFillZero(hhmm);
        }
        return res;
    }
}